﻿using Essensoft.Paylink.Alipay;
using Essensoft.Paylink.Alipay.Domain;
using Essensoft.Paylink.Alipay.Request;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Text;

namespace D.Medicalcare.Write.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ApipayController : ControllerBase
    {

        private readonly IAlipayClient _client;
        private readonly IOptions<AlipayOptions> _optionAccessor;

        public ApipayController(IAlipayClient _client, IOptions<AlipayOptions> _optionAccessor)
        {
            this._client = _client;
            this. _optionAccessor = _optionAccessor;
        }

        /// <summary>
        /// 电脑网站支付
        /// </summary>
        /// <param name="viewModel"></param>
        [HttpPost]
        public async Task<IActionResult> PagePay(string OrderCode,decimal money)
        {
            var model = new AlipayTradePagePayModel
            {
                //说明
                Body = $"支付订单编号为:{OrderCode},金额:${money}",
                //内容
                Subject = $"编号为:{OrderCode}订单支付",
                //支付内容
                TotalAmount = money.ToString(),
                //订单编号
                OutTradeNo = OrderCode,
                //企业签约码，目前仅支持FAST_INSTANT_TRADE_PAY
                ProductCode = "FAST_INSTANT_TRADE_PAY"
            };
            var req = new AlipayTradePagePayRequest();
            ////支付
            //req.SetBizModel(model);
            //req.SetNotifyUrl(http://za26bdc5.natappfree.cc);
            //req.SetReturnUrl(viewModel.ReturnUrl);

            var response = await _client.PageExecuteAsync(req, _optionAccessor.Value);
            return Content(response.Body, "text/html", Encoding.UTF8);
        }
    }
}
